package com.yunhe.realtime.config;

import com.yunhe.realtime.websocket.CustomHandler;
import com.yunhe.realtime.websocket.RealTimeObjectHandler;
import com.yunhe.realtime.websocket.RealtimeHandler;
import com.yunhe.realtime.websocket.SystemTimeHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * WebSocket配置类
 * @author liuronglei
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(realtimeHandler(), "/measurements/realtime/socket")
                .setAllowedOrigins("*");
        registry.addHandler(customHandler(), "/measurements/realtime/custom/socket")
                .setAllowedOrigins("*");
        registry.addHandler(systemTimeHandler(), "/measurements/realtime/systemTime/socket")
                .setAllowedOrigins("*");
        registry.addHandler(realtimeObjectHandler(), "/measurements/realtime/object/socket")
                .setAllowedOrigins("*");
    }

    @Bean
    public WebSocketHandler realtimeHandler() {
        return new RealtimeHandler();
    }

    @Bean
    public WebSocketHandler customHandler() {
        return new CustomHandler();
    }

    @Bean
    public WebSocketHandler systemTimeHandler() {
        return new SystemTimeHandler();
    }

    @Bean
    public WebSocketHandler realtimeObjectHandler() { return new RealTimeObjectHandler(); }
}